package org.bbelovic.devel.pexeso.gui;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class GameController {

    private final Collection<GameListener> listeners;
    private final List<Player> players;
    private int playerIndex;
    private FlagLabelsManager flagManager;
    

    public GameController() {
       this.listeners = new ArrayList<GameListener>();
       this.players = new ArrayList<Player>();
    }
    
    public Player getActualPlayer() {
        return players.get(playerIndex);
    }
    
    public boolean hasPlayers() {
        return !players.isEmpty();
    }
    
    public void nextPlayer() {
        if (playerIndex < players.size()-1) {
            ++playerIndex;
        } else {
            playerIndex = 0;
        }
    }
    
    String getScoreInformation() {
        final StringBuilder sb = new StringBuilder();
        for (Player eachPlayer: players) {
            sb.append(eachPlayer.toString())
              .append(" ");
        }
        return sb.toString();
    }

    boolean verifyFlagLabel(final String flagLabel) {
        final Player p = getActualPlayer();
        if (!flagManager.isFlagIconAvailable(flagLabel)) {
            return false;
        } else if (!p.getPairs().isEmpty() && containsFlagLabel(flagLabel)) {
            return false;
        } else {
            return true;
        }
    }

    private boolean containsFlagLabel(final String flagLabel) {
        Player p = getActualPlayer();
        return p.getPairs().contains(flagLabel);
    }

    private boolean canContinue(final Player player) {
        final List<String> pairs = player.getPairs();
        final String first = pairs.get(0).substring(0, pairs.get(0).lastIndexOf("-"));
        final String second = pairs.get(1).substring(0, pairs.get(1).lastIndexOf("-"));
        if (first.equals(second)) {
            player.addScorePoint(1);
            return true;
        }
        return false;
    }
    
    void evaluateFlagLabels(String label) {
        final Player p = getActualPlayer();
        flagManager.turnFlagIconOn(label);
        p.decrementActionPoints();
        p.addPair(label);
        final String msg = "Player "+ p +" turned flag "+ label.substring(0, label.lastIndexOf("."));
        System.out.println(msg);
        if (p.getActionPoints() == 0) {
            if (canContinue(p)) {
                flagManager.removeFlagLabelWithId(p.getPairs());
                p.emptyPairs();
                p.restoreActionPoints();
            } else {
                System.out.println("Enabling switch button");
                updateComponents();
            }
        }
    }

    private void updateComponents() {
        for (final GameListener eachListener: listeners) {
            eachListener.updateComponents();
        }
    }

    void addGameListener(final GameListener gameListener) {
        listeners.add(gameListener);
    }

    // setters & getters

    void setFlagManager(final FlagLabelsManager flagManager) {
        this.flagManager = flagManager;
    }
    
    FlagLabelsManager getFlagManager() {
        return this.flagManager;
    }

    void addPlayer(final Player player) {
        players.add(player);
    }
    
    List<Player> getPlayers() {
        return Collections.unmodifiableList(this.players);
    }
}
